package cn.cloudplug.friendsrecommend.fragment;



import org.json.JSONObject;

import com.umeng.socialize.bean.SHARE_MEDIA;

import cn.cloudplug.friendsrecommend.FindPasswordActivity;
import cn.cloudplug.friendsrecommend.R;
import cn.cloudplug.friendsrecommend.entity.MySelfInfomation;
import cn.cloudplug.friendsrecommend.util.Constant;
import cn.cloudplug.friendsrecommend.util.HttpTast;
import cn.cloudplug.friendsrecommend.util.LoginOrRegisterUtil;
import android.content.Intent;
import android.graphics.Paint;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class LoginFragment extends Fragment implements OnClickListener {
	private EditText et_name;
	private EditText et_pwd;
	private Button btn_login;
	private TextView tv_forget_pwd;
	private MySelfInfomation me;
	public LoginFragment() {
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		View view = inflater.inflate(R.layout.activity_login, container, false);
		tv_forget_pwd = (TextView) view.findViewById(R.id.tv_forget_pwd);
		tv_forget_pwd.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);
		et_name = (EditText) view.findViewById(R.id.et_username);
		et_pwd = (EditText) view.findViewById(R.id.et_password);
		btn_login = (Button) view.findViewById(R.id.btn_login);
		btn_login.setOnClickListener(this);
		tv_forget_pwd.setOnClickListener(this);
		view.findViewById(R.id.iv_qq).setOnClickListener(this);
		view.findViewById(R.id.iv_wx).setOnClickListener(this);

		return view;
	}

	@Override
	public void onClick(View v) {
		LoginOrRegisterUtil loginOrRegisterUtil = new LoginOrRegisterUtil(
				getActivity());
		switch (v.getId()) {
		case R.id.btn_login:

			doLogin();
			break;
		case R.id.tv_forget_pwd:
			getActivity().startActivity(
					new Intent(getActivity(), FindPasswordActivity.class));
			break;
		case R.id.iv_qq:
			loginOrRegisterUtil.login(SHARE_MEDIA.QQ);
			break;
		case R.id.iv_wx:
			loginOrRegisterUtil.login(SHARE_MEDIA.WEIXIN);
			break;
		}
	}
	
	private void login(){
		Intent intent = new Intent(Constant.LOGIN_SUCCESS);
		intent.putExtra("MySelfInfomation", me);
		getActivity().sendBroadcast(intent);
		getActivity().finish();
		getActivity().overridePendingTransition(R.anim.activity_out,R.anim.b_activity_out);
	}

	// 登录按钮点击事件
	public void doLogin() {
		final String name = et_name.getText().toString().trim();
		final String pwd = et_pwd.getText().toString().trim();
		if (TextUtils.isEmpty(name)) {
			et_name.setError("用户名不能为空");
			return;
		}
		if (TextUtils.isEmpty(pwd)) {
			et_pwd.setError("密码不能为空");
			return;
		}

		try {
			JSONObject result=new HttpTast(getActivity()).execute("http://101.227.72.203/api/action.php", "mLogin", name, pwd).get();
			String status=result.getString("status");
			if(status.equals("0")){
				String ID=result.getString("ID");
				String phoneNumber=result.getString("PhoneNumber");
				String password=result.getString("Password");
				String nick=result.getString("Nick");
				String realName=result.getString("RealName");
				String gender=result.getString("Gender");
				String countryID=result.getString("CountryID");
				String iDNumber=result.getString("IDNumber");
				String phoneNumber2=result.getString("PhoneNumber2");
				String emergency=result.getString("Emergency");
				String emergencyPhone=result.getString("EmergencyPhone");
				String birthday=result.getString("Birthday");
				String height=result.getString("Height");
				String medicalInsuranceNo=result.getString("MedicalInsuranceNo");
				String faceSrc=result.getString("FaceSrc");
				String joinTime=result.getString("JoinTime");
				me=new MySelfInfomation(ID, phoneNumber, password, nick, realName, gender, countryID, iDNumber, phoneNumber2, emergency, emergencyPhone, birthday, height, medicalInsuranceNo, faceSrc, joinTime);
				Log.i("TAG", me.toString());
			}else{
				Toast.makeText(getActivity(), "帐号或密码错误,测试登录中。。。", 1).show();
				me=new MySelfInfomation();
				me.setID("1");
				Log.i("TAG", me.toString());
				login();
			}
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
	}
}
